#! /usr/bin/env python
import rospy
from turtlesim.srv import Spawn, SpawnRequest, SpawnResponse
from geometry_msgs.msg import Twist
import time
from voice2word import voice_record, voice_translate
from chatglm_ros import chat_ros_client
import threading

class TurtleBot:
    def userinput2word(self):
        audio_path = "/home/fwy/桌面/课设/chat_turtle2/src/voice2word/audio.wav"
        voice_record.record_audio(audio_path)
        self.text_list = voice_translate.get_words(audio_path)
         
    
    def chatglm_process(self):
        action_client = chat_ros_client.ChatClient(self.text_list)
        return chat_ros_client.ChatClient.getcode(action_client)
    
    def interface(self):
         print("交互开始")
         self.userinput2word()
         self.code = self.chatglm_process()
         print("交互结束")

    
    def getorder(self):
        return self.code

    def circle(self):
        # 创建发布者对象
          circlepub = rospy.Publisher("turtle1/cmd_vel", Twist, queue_size=1)
          # 组织数据并发布数据
          # 设置发布频率
          rate = rospy.Rate(10)
          # 创建速度消息
          twist = Twist()
      
          twist.linear.x = 0.5
          twist.linear.y = 0.0
          twist.linear.z = 0.0
      
          twist.angular.x = 0.0
          twist.angular.y = 0.0
          twist.angular.z = 0.5
          while True:
             print("转弯中")
             rate.sleep()
             circlepub.publish(twist)
    
    def forward(self):
        # 创建发布者对象
          forwardpub = rospy.Publisher("turtle1/cmd_vel", Twist, queue_size=1)
          # 组织数据并发布数据
          # 设置发布频率
          rate = rospy.Rate(10)
          # 创建速度消息
          twist = Twist()
      
          twist.linear.x = 0.5
          twist.linear.y = 0.0
          twist.linear.z = 0.0
      
          twist.angular.x = 0.0
          twist.angular.y = 0.0
          twist.angular.z = 0.0
          while True:
              print("前行中")
              rate.sleep()
              forwardpub.publish(twist)
  

def main():
    rospy.init_node("chat_ros_turtle_client")
    a_turtle = TurtleBot()
    a_turtle.interface()
    code = a_turtle.getorder()
    circle_thread = threading.Thread(target=a_turtle.circle)
    forward_thread = threading.Thread(target=a_turtle.forward)

    if code is not None:
           try:
                exec(code)
           except Exception as e:
                print("error")
    
  
if __name__ == '__main__':
    main()
